<?php
/*
	代理商地区管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'list';
$tabs=array(
	array('op'=>'list','title'=>'代理地区列表'),
	array('op'=>'list_check','title'=>'待审核代理商'),
);
set_html('tabs',$tabs);
$return['statuss']=array(
		'0'=>'待审核',
		'1'=>'审核通过',
		'2'=>'审核不通过',
);
$return['stops']=array(
		'0'=>'正常',
		'1'=>'禁用',
);
$return['types']=$this->agent_types();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		//选择代理商
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['pcd']){
		$pcd=cfc('tools')->base64_decode($_RQ['pcd'],$_SESSION['base_key_str']);
		
		$pcd=json_decode($pcd,true);
		$return['params']['pcd']=$pcd;
		//根据选择的省市区查找代理商
		if($pcd['province']){
			//省级代理
			$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$pcd['province']),'code');
			$type=1;
		}
		if($pcd['city']){
			$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$pcd['city']),'code');
			$type=2;
		}
		if($pcd['district']){
			$type=3;
			$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$pcd['district']),'code');
		}
		if($code){
			$codes=array($code);
			if($type==1){
				$c_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($c_codes as $c_code){
					$codes[]=$c_code['code'];
					$d_codes=pdo_getall('core_area',array('parent_code'=>$c_code),array('code'));
					if($d_codes){
						foreach($d_codes as $d_code){
							$codes[]=$d_code['code'];
						}
					}
				}
			}
			if($type==2){
				$d_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($d_codes as $d_code){
					$codes[]=$d_code['code'];
					
				}
			}
		}
		if($codes){
			$params['area_code']=$codes;
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('agent',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['area_code']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			$item['url']=$this->get_agent_login_url($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			array('name'=>'type','title'=>'代理商类型','type'=>'radio','options'=>'types'),
			array('name'=>'area_code','title'=>'代理区域','type'=>'string'),
			array('name'=>'createtime','title'=>'代理时间','type'=>'time'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'stop','title'=>'是否禁止','type'=>'radio','options'=>'stops'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁止','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'启用','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'change','title'=>'调整代理地区'),
			array('type'=>'modal','op'=>'cpassword','action'=>'member.member','plugin'=>'core','title'=>'重置密码'),
			array('type'=>'open','name'=>'url','title'=>'进入代理后台'),
		)),
		'search'=>array(
			array('type'=>'choose','name'=>'mid','title'=>'代理商','action'=>'member.get_member','plugin'=>'core'),
			array('type'=>'pcd','name'=>'pcd','title'=>'地区','options'=>'areas'),
		),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='list_check'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>0);
	if($_RQ['mid']){
		//选择代理商
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['pcd']){
		$pcd=cfc('tools')->base64_decode($_RQ['pcd'],$_SESSION['base_key_str']);
		
		$pcd=json_decode($pcd,true);
		$return['params']['pcd']=$pcd;
		//根据选择的省市区查找代理商
		if($pcd['province']){
			//省级代理
			$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$pcd['province']),'code');
			$type=1;
		}
		if($pcd['city']){
			$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$pcd['city']),'code');
			$type=2;
		}
		if($pcd['district']){
			$type=3;
			$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$pcd['district']),'code');
		}
		if($code){
			$codes=array($code);
			if($type==1){
				$c_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($c_codes as $c_code){
					$codes[]=$c_code['code'];
					$d_codes=pdo_getall('core_area',array('parent_code'=>$c_code),array('code'));
					if($d_codes){
						foreach($d_codes as $d_code){
							$codes[]=$d_code['code'];
						}
					}
				}
			}
			if($type==2){
				$d_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($d_codes as $d_code){
					$codes[]=$d_code['code'];
					
				}
			}
		}
		if($codes){
			$params['area_code']=$codes;
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('agent',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['area_code']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			$item['url']=$this->get_agent_login_url($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			array('name'=>'type','title'=>'代理商类型','type'=>'radio','options'=>'types'),
			array('name'=>'area_code','title'=>'代理区域','type'=>'string'),
			array('name'=>'createtime','title'=>'代理时间','type'=>'time'),
			array('name'=>'stop','title'=>'代理状态','type'=>'radio','options'=>'stops'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核'),
		)),
		'search'=>array(
			array('type'=>'choose','name'=>'mid','title'=>'代理商','action'=>'member.get_member','plugin'=>'core'),
			array('type'=>'pcd','name'=>'pcd','title'=>'地区','options'=>'areas'),
		),
	);
	$return['htmls']=$htmls;
	$return['areas']=cfc('tools')->pcd();
	exi($return);
}
//审核代理商
//审核代理商
if($_RQ['op']=='check'){
	
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'status'=>intval($post['status']),
		);
		$this->update('agent',$update,array('id'=>intval($post['id'])));
		exi('审核成功！','','list_check');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择代理商！','error');
	}
	$item=$this->get('agent',array('id'=>$id));
	$item['nickname']=pdo_getcolumn('core_members',array('id'=>$item['mid']),'nickname');
	$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核代理商',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'代理商昵称','read'=>true),
			array('type'=>'string','name'=>'address','title'=>'代理地区','read'=>true),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
		),
	);

	$return['areas']=$this->get_agent_areas($_SESSION['uid']);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	
	$return['item']=$item;
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='change'){
	//调整代理地区
	
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择代理商！','error');
		}
		$data=array();
		if($post['pcd']){
			if($post['pcd']['province']){
				//省级代理
				$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$post['pcd']['province']),'code');
				$type=1;
			}
			if($post['pcd']['city']){
				$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$post['pcd']['city']),'code');
				$type=2;
			}
			if($post['pcd']['district']){
				$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$post['pcd']['district']),'code');
				$type=3;
			}
			$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$code),'id');
			if($check){
				exi('该地区已存在代理商，请选择其他地区！','error');
			}
			$data['type']=$type;
			$data['area_code']=$code;
		}
		if(!$data){
			exi('请先选择地区！','error');
		}
		$this->update('agent',$data,array('id'=>$id));
		exi('设置代理商成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$item=$this->get('agent',array('id'=>$id),array('id','area_code'));
	$areas=pdo_getall('core_area',array('parent_code'=>'000000000000'),array('code','title'));
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'pcd','name'=>'pcd','title'=>'设置代理地区','options'=>'areas'),
		),
	);
	$return['areas']=cfc('tools')->pcd();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='stop'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$this->update('agent',array('stop'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('禁止成功！','','list');
}
if($_RQ['op']=='start'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$this->update('agent',array('stop'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('启用成功！','','list');
}
if($_RQ['op']=='detail'){
	//显示代理商详细信息
	
}
if($_RQ['op']=='del'){
	//删除代理商
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择代理商！','error');
	}
	$this->delete('agent',array('id'=>$id));
	exi('删除成功！','','list');
}